package com.session06.am.cainiao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Login extends JFrame {

    private JPanel p1, p2, p3;
    private JTextField txtName;
    private JPasswordField txtPwd;
    private JButton btnLogin, btnCancel;
    public Boundar boundar;
    private int num = 0;

    Staff[] staff = new Staff[10];
    Express[] expresses = new Express[10];

    public Staff[] getStaff() {
        return staff;
    }

    public void setStaff(Staff[] staff) {
        this.staff = staff;
    }

    public Express[] getExpresses() {
        return expresses;
    }

    public void setExpresses(Express[] expresses) {
        this.expresses = expresses;
    }

    public JPasswordField getTxtPwd() {
        return txtPwd;
    }

    public void setTxtPwd(JPasswordField txtPwd) {
        this.txtPwd = txtPwd;
    }

    public JTextField getTxtName() {
        return txtName;
    }

    public void setTxtName(JTextField txtName) {
        this.txtName = txtName;
    }

    public Login() {
        staff[0] = new Staff("张三", "111", "员工");
        staff[1] = new Staff("李四", "222", "管理员");

        expresses[0] = new Express("001", "张某", "123456789", "2022-02-08", "未取货", "无", "无");
        expresses[1] = new Express("002", "李某", "987654321", "2022-02-08", "未取货", "无", "无");
        expresses[2] = new Express("003", "王某", "147258369", "2022-02-08", "未取货", "无", "无");
        boundar = new Boundar();
    }

    public class Boundar extends JFrame {

        public Boundar() {
            this.setTitle("登陆界面");
            this.setBounds(300, 100, 350, 250);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            this.setLayout(new GridLayout(3, 1, 5, 5));

            p1 = new JPanel();
            JLabel lblName = new JLabel("请输入用户名");
            txtName = new JTextField(15);
            p1.add(lblName);
            p1.add(txtName);

            p2 = new JPanel();
            JLabel lblPwd = new JLabel("请输入密码");
            txtPwd = new JPasswordField(15);
            p2.add(lblPwd);
            p2.add(txtPwd);

            p3 = new JPanel();
            btnLogin = new JButton("登录");
            btnCancel = new JButton("取消");
            p3.add(btnLogin);
            p3.add(btnCancel);

            this.add(p1);
            this.add(p2);
            this.add(p3);

            btnLogin.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String username = txtName.getText();
                    String password = new String(txtPwd.getPassword());
                    int flag = -1;
                    for (int i = 0; i < staff.length; i++) {
                        if (staff[i] != null) {
                            if (staff[i].getUsername().equals(username) && staff[i].getPassword().equals(password)) {
                                flag = i;
                                break;
                            }
                        }
                    }
                    if (flag != -1) {
                        String level = staff[flag].getLevel();
                        if (level.equals("管理员")) {

                            ManageService manageService = new ManageService(Login.this);
                            Boundar.this.dispose();

                        } else {
                            StaffService staffService = new StaffService(Login.this);
                            Boundar.this.dispose();
                        }

                    } else {
                        JOptionPane.showConfirmDialog(null, "用户名或密码错误");
                        num++;
                    }
                    //3次机会
                    if (num == 3) {
                        JOptionPane.showConfirmDialog(null, "非法用户");
                        System.exit(0);
                    }
                }
            });
            this.setVisible(true);
        }
    }
}
